<?php

namespace app\api\controller;
use think\Request;

class Xunfei extends ApiBase
{
    private $appid;
    private $secret_key;
    private $upload_file_path;
    private $ts;
    public function __construct(Request $request){
//        parent::__construct($request);

       // $this->upload_file_path = "./upload/shiping.mp4";
        $this->appid = "ff46b8ce";
        $this->secret_key = "17c1ffdf4538a48bd8f4f13a34d27655";
        $this->ts = time();


    }

    /**
     * 得到结果
     */
    public function getResult(){

        $params = input();
        if (!isset($params['orderID']) || $params['orderID'] == ''){

            $this->fail('缺少orderID');
        }
        $orderId = $params['orderID'];

        $signa = $this->getSigna();
        $url = "http://raasr.xfyun.cn/v2/api/getResult";
        $url .= "?signa={$signa}&orderId=".$orderId."&resultType=transfer&appId=".$this->appid."&ts=".$this->ts;

        $header = array("Content-Type: application/json; charset=UTF-8");
        $re = $this->curl_request($url,"POST","",$header);
        $res = json_decode($re,true);
        if ($res['code'] == "000000"){

            $res11 = json_decode($res['content']['orderResult'],true);
            $words = [];
            foreach ($res11['lattice'] as $vv){

                $words[] = $vv['json_1best'];
            }
            halt($words);
            $words_result = '';
            foreach ($words as $v){

                foreach ($v['st']['rt'] as $vv) {

                    foreach ($vv as $kkk=>$vvv) {

                        if (is_array($vvv)){

                            $words_result.= $vvv[0]['cw'][0]['w'];
                        }
                        //foreach ($vvv as $vvvv){

                            //$words_result.= $vvv[0]['cw'][0]['w'];
                        //}
                    }
                }

            }
            //return $words_result;
            $this->successful('获取结果成功',$words_result);
        }else{

            $this->fail('获取结果失败',$res);
        }

    }

    /**
     *
     * 上传文件
     */
    public function upload() {
//           require_once '../extend/getid3/getid3.php';
//        if($this->upload_file_path != ''){
            $url = "https://raasr.xfyun.cn/v2/api/upload";
            $file_url = 'http://jdvodlhltdtya.vod.126.net/jdvodlhltdtya/7005800-1348098866595815-1720148924328-0.mp4';
//            $getId = new \getID3();
            $fileInfo = get_headers($file_url,true);
            $filesize = $fileInfo['Content-Length'];

           // $callbackUrl = 'http://aiwz.mguit.com/api/xunfei/getResult';
            $signa = $this->getSigna();
            $url .= "?duration=2&signa=".$signa."&fileName=".(basename($file_url))."&fileSize=".$filesize."&sysDicts=uncivilizedLanguage&roleType=1&roleNum=10&audioMode=urlLink&audioUrl={$file_url}&appId=".$this->appid."&ts=".$this->ts;

            $fp = fopen($file_url,'r');
            $body = fread($fp,$filesize);
            fclose($fp);

            $header = array("Content-Type: application/json; charset=UTF-8,Chunked: false");
            $res = $this->curl_request($url,"POST",$body,$header);
            $res = json_decode($res,true);

            if ($res['code'] == "000000"){

                $this->successful('上传文件成功',$res['content']['orderId']);
            }else{

            $this->fail('上传文件失败',$res);
           }



    }
    /**
     * 签名
     */
    public function getSigna(){
        $baseString = md5($this->appid.$this->ts);
        $signa = hash_hmac('sha1',$baseString,$this->secret_key,true);
        $signa=base64_encode($signa);
        return $signa;

    }
    /**
     * CURL请求
     * @param string $url
     * @param string $method
     * @param array $params
     * @param array $headers
     */
    public function curl_request($url = '',$method = 'POST',$params = array(),$headers = array()){
        $method = strtoupper($method);

        //初始化
        $ch     = curl_init();
        //    在HTTP请求头中"Referer: "的内容。
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名
        curl_setopt($ch, CURLOPT_SSLVERSION, 0);  //传递一个包含SSL版本的长参数。

        if($method == 'GET' && !empty($params) && is_array($params)){
            $url .= '?'.http_build_query($params);

        }else if ($method == 'POST') {
            //启用时会发送一个常规的POST请求，类型为：application/x-www-form-urlencoded，就像表单提交的一样。
            curl_setopt($ch, CURLOPT_POST,true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

        }else if ($method == 'PUT') {
            //启用时允许HTTP发送文件，必须同时设置CURLOPT_INFILE和CURLOPT_INFILESIZE。
            curl_setopt($ch, CURLOPT_PUT, true);
        }
        //CURLOPT_SSL_VERIFYPEER(默认值为2)被启用，CURLOPT_SSL_VERIFYHOST需要被设置成TRUE否则设置为FALSE。
        //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_URL, $url);

        //返回的内容作为变量储存，而不是直接输出。必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //设置为0表示不返回HTTP头部信息
        curl_setopt($ch, CURLOPT_HEADER, false);
        //在发起连接前等待的时间，如果设置为0，则无限等待
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);

        if(!empty($headers)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        //启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器，使用CURLOPT_MAXREDIRS可以限定递归返回的数量。
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($ch, CURLOPT_TIMEOUT, 0);

        //设置为1表示稍后执行的curl_exec函数的返回是URL的返回字符串，而不是把返回字符串定向到标准输出并返回TRUE
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //执行并获取结果
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);

        $err = curl_error($ch);
        //释放curl句柄
        curl_close($ch);



        return $result;

    }


}